Liberty Basic Beginner Series - Part 6

Creating Windows

© 2003, Brad Moore

Moving Windows -

We seem to have covered a lot of new material in just responding to a couple of the challenges presented in the last installment. This would make a good place to quit, but I am eager to introduce the topic of windows. So hang on while I attempt to roll back some of the fog and mystery surrounding the creation of a window in Liberty Basic.

Well to hear me put it that way one would imagine that there is a lot of scary stuff and perhaps a bit of science and magic involved in creating a window, but it is really quite simple. A person can actually do it in just one line of code. The details get a little bit more gritty when we begin to consider the ways we can enhance and modify the window.

But first, what is a window anyway? I am going to presume you know something of the operating system in which Liberty Basic runs, specifically Microsoft Windows - that you have seen windows. A window is a special container which allows presentation of material in graphic form using fonts, graphic shapes and colors. You are (most likely) reading this in a window which is part of some kind of a program. In some languages we call the window a form. Think of it as such. A piece of paper which you can open up, add things to and then later destroy. It has a top area for a title as well as sides which define the boundaries of the form. Most programs have "extras" adorning their windows, like menus, button bars and scroll bars. Here is a very simple window I created with Liberty Basic:

This window was created with just two lines of code. Only one line was needed to actually open the window, but without the other one the window would have closed as soon as it opened. Here is that simple program;

Open "My window" for Window as #main
Wait

You can copy the program, paste it into Liberty Basic and run it for yourself.

Lets discover what is going on here. The command that is used to open a window is OPEN (seems predictable). We have dealt with the OPEN command in the context of file operations earlier in this article. Now we are going to open a window. If you study the syntax you will notice that the open command conforms to the same pattern of arguments as it did when used with file operations.

Specifically there is the OPEN command which is followed by a string. In the case of the file open, the string was the filepath (a fully qualified filename with its path). In the case of the OPEN statement for a window, it is simply the text string you want to have appear in the window's titlebar. While it is not optional, you can pass it an empty string if you choose.

Next we have the mode in which the window was opened. It is part of the "for" clause. There are quite a few options in this category for opening a window. If you are not sure, use the type "WINDOW". Here is what the help file says about the many window types:

Liberty BASIC provides different kinds of window types, to which you can add as many controls as needed (see help section Controls - Menus, Buttons, Etc.). Here are their kinds and the commands associated with them:

The way that you would specify what kind of window to open would be as follows:

    open "Window Title" for type as #handle

where type would be one of the descriptors below (from the help file):

Style suffixes for window types (not all suffixes are supported for each window type):

_fs			window is sized to fill the screen
_nf			window has no frame and cannot be resized by user
_nsb			window doesn't contain scroll bars
_ins			contains inset texteditor
_popup		window contains no titlebar or sizing frame
_modal			window must be closed before another window can gain focus

Window types:

graphics		open a graphics window
graphics_fs		open a graphics window full screen (size of the screen)
graphics_nsb		open a graphics window w/no scroll bars
graphics_fs_nsb	open a graphics window full screen, w/no scroll bars
graphics_nf_nsb	open a graphics window with no sizing frame or scroll bars

text			open a text window
text_fs			open a text window full screen
text_nsb		open a text window w/no scroll bars
text_nsb_ins		open a text window w/no scroll bars, with inset editor

window		open a basic window type
window_nf		open a basic window type without a sizing frame
window_popup	open a window without a titlebar

dialog			open a dialog box
dialog_modal		open a modal dialog box
dialog_nf		open a dialog box without a frame
dialog_nf_modal	open a modal dialog box without a frame
dialog_fs		open a dialog box the size of the screen
dialog_nf_fs		open a dialog box without a frame the size of the screen
dialog_popup		open a dialog box without a titlebar

Don't get bogged down in the details of the window type. Use the type WINDOW, unless you want to do full window graphics, in which case use the type GRAPHICS. Some of the modifiers come in handy, but are not essential. It does not hurt to play around with the many varieties of window types and I encourage experimentation here. Don't feel bad if this overwhelms you. Use the basic types and leave the rest for when you get a little bit more practice using Liberty Basic and want to spread your wings a bit.

The last part of the call is the HANDLE. The handle (or window handle) is like the file handle we discussed earlier. It is simply a named thing you create. It is use to reference your window in other commands. It might be interesting to consider the following analogy if you are struggling with the concept.

Jane Doe is given a Social Security Number. It is unique. It identifies this Jane Doe specifically. In a near by town there lives another Jane Doe. She too has a Social Security Number, but her number is different from the first Jane Doe we met. Consider what would happen ff both Jane Does were in the same room. Sure they would look different, but if you had never met either one, and you called them by name, to which one are you referring? How ever using the alternate (and unique) method of identification (the Social Security Number) you would be able to specify the exact one you are interested in.

Windows can have any name. It is possible to have many windows open at the same time each with the same name in the titlebar. Each window, however, must have a unique handle. It is like an id card. The handle allows us to specify which window we want to deal with regardless of the window title. As the programmer we actually create this handle. It is our job to insure each is unique. Liberty Basic and Windows will maintain the association between the actual window and the handle behind the scenes.

Many Liberty Basic programmers follow an unwritten convention of calling the primary window of their program #main. You do not have to do this. I tend to do this out of habit. All handles must however begin with a pound sign "#".

My little program included another statement - specifically the WAIT command. This basically is an alias for INPUT. It is the preferred command most Liberty Basic programmers use with graphics oriented programming (over INPUT). You could use the command INPUT A$ if you want. They both do the same thing - they cause the program execution to stop until the user interacts with the computer.

Previously we used INPUT to actually get some data from the user. The windows environment is a very different world from the console based world we have been living in up until now. I am not prepared to explain all the differences now - there are many and they are fundamental to succeeding when programming with windows. Let me just say that even though INPUT is an acceptable command in place of wait, it will never actually collect any data from the user. It can not, because it exists in the console window yet our window we just created and displayed is the window with "focus". It is on top and it is the only one that can get input for the moment. This is the nature of an "Event Driven" environment, which is what window is. We will discuss these concepts more later. For now we are going to do some brute force instruction.

Since INPUT has no effect, and WAIT is more descriptive of what is really happening (we are waiting for an event), I recommend using it. We are not going to go into very much depth here on Event Driven programming. It is a subject that can fill its own article or two (in fact I have published two articles on the subject elsewhere in earlier newsletters.)

What we do want to do is talk a bit more about this concept of window creation and some of the options that are available to us.

Do you do windows too?

We can do an awful lot to customize our windows that we are working with. We can set their size, their frame features, their type, their default colors as well as add the gadgets that go onto the window - like buttons and menus and stuff. Much of this must be setup BEFORE the window has been opened. Lets look at a few of these items as we work toward completing a neat little project - a graphic set of dice that we can "throw".

Earlier we opened a simple "WINDOW" type window. It is usually the best bet when you are unsure which window type to use. In the case of our project we are going to do some graphics oriented stuff, so a graphics type of window will work better. Looking at the list above I see there are five of these to choose from. For now we will simply select the simplest one - GRAPHICS.

A graphics window is a special window that is designed to support the drawing of graphics on it, such as lines, circles, boxes as well as graphic files (bitmaps). You can even draw text on them. They can also have some of the various types of controls placed on them as well. To this end they are fairly flexible. Lets title our window "Dice". We can open it with the following command (add a WAIT command so you can see you window for more than a second):

open "Show Dice" for graphics as #main
wait

Did you notice the scroll bars on the right side and bottom of the window? These are often distracting and in our case we don't want these. They are easy to get rid of simply by specifying the mode as having no scroll bars or "_nsb". This is a window modifier. Change the code so that the mode is opened as graphics_nsb. It should look like the following:

Open "Show Dice" for Graphics_nsb as #main
wait

That seems to work pretty well, but the window does not fit my needs size ways very well. I need it shorter and narrower. Luckily Liberty Basic has a couple global variables that can be used to set the width and height of the window. It must be specified before the window is opened. The variables are WindowWidth and WindowHeight. These are measured in screen pixels.

There are a couple additional variables that can be used to specify the placement of the window on the desktop. The allow the programmer to set the position in screen pixels of upper left corner of the window. It is possible to center the window, but for now we will simply open it at 40,40.

Here they is my program utilizing these new settings:

WindowWidth = 740
WindowHeight = 160
UpperLeftX = 40
UpperLeftY = 40

Open "Show Dice" for Graphics_nsb as #main
wait


Home

Tip Corner

Plot 3d

Memory Mapped File

Random File Selector

Notes for Beginners

No SoundBlaster Board

PrintInstalled Fonts

Essential Libby

API Drive Info

Begginer Series 6

Newsletter help

Index